package com.onepower.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onepower.core.domain.request.MatchRefereeForm;
import com.onepower.core.domain.response.MatchCheckInfo;
import com.onepower.core.domain.response.RefereeInfo;
import com.onepower.core.modules.repository.entity.MatchReferee;
import com.onepower.core.modules.service.IMatchRefereeService;
import com.onepower.core.modules.service.IMatchScheduleService;
import com.onepower.core.result.CommonResult;
import com.onepower.core.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 比赛-场地-裁判关联表 前端控制器
 * </p>
 *
 * @author kin
 * @since 2018-08-06
 */
@RestController
@RequestMapping("/api/match/referee")
public class MatchRefereeController {

    @Autowired
    private IMatchRefereeService matchRefereeService;

    @Autowired
    private IMatchScheduleService matchScheduleService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(value = "matchId", required = false, defaultValue = "0") int matchId) {
        List<RefereeInfo> refereeList = matchRefereeService.getRefereeList(matchId);
        return ResultUtils.ok(refereeList);
    }

    @RequestMapping(value = "restLoginState/{id}", method = RequestMethod.POST)
    public CommonResult restLoginState(@PathVariable("id") Integer id) {
        matchRefereeService.updateRefereeLoginType(id, false);
        return ResultUtils.ok();
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    public CommonResult save(@RequestBody MatchRefereeForm matchRefereeForm) {
        matchRefereeService.save(matchRefereeForm);
        return ResultUtils.ok();
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public CommonResult delete(@RequestBody Integer[] ids) {
        matchRefereeService.delete(ids);
        return ResultUtils.ok();
    }

    @RequestMapping(value = "checklist", method = RequestMethod.GET)
    public CommonResult getCheckSchedule(@RequestParam(value = "matchPlaceId", required = false, defaultValue = "0") int matchPlaceId,
                                         @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize", required = false, defaultValue = "20") int pageSize) {
        Page<MatchCheckInfo> matchScheduleCheckPage = matchScheduleService.getMatchScheduleCheckPage(new Page<>(pageNum, pageSize), matchPlaceId);
        return ResultUtils.ok(matchScheduleCheckPage);
    }

    @ResponseBody
    @RequestMapping(value = "/updateTime", method = RequestMethod.GET)
    public CommonResult updateTime(@RequestParam int refereeId) {
        MatchReferee matchReferee = new MatchReferee();
        matchReferee.setId(refereeId);
        matchReferee.setCheckTime(new Date());
        return matchRefereeService.updateById(matchReferee) ? ResultUtils.ok() : ResultUtils.error();
    }

}
